package com.erp.myAdapter

import android.annotation.SuppressLint
import android.content.Context
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.LinearLayout
import androidx.core.content.ContextCompat
import com.erp.R
import com.erp.bean.PhotoBean
import com.erp.util.Myutil
import com.erp.util.Sputil
import kotlinx.android.synthetic.main.lv_iv_item.view.*

/**
 * Created by Administrator on 2019/4/16/016.
 * 作者：刘洋
 */
class PhotoAdapter(var context: Context, var list : MutableList<PhotoBean> ,var isadd : Boolean) : BaseAdapter(){
    private val minflater: LayoutInflater = LayoutInflater.from(context)
    internal var width = 0
    init {
        width = Sputil.sp("erp").getInt("width", width)
    }
    @SuppressLint("ViewHolder")
    override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View {
        val v :View = minflater.inflate(R.layout.lv_iv_item, null)
        val iv = v.iv_item

        val params = iv.layoutParams as LinearLayout.LayoutParams
        params.height = width / 3
        iv.layoutParams = params
        Log.d("PhotoAdapter",list.toString())
        if (position == list.size) {
            v.tv_time.visibility=View.GONE
            iv.setImageResource(R.mipmap.erp_photo)
        } else {
            v.tv_time.visibility=View.VISIBLE
            Myutil.showhttpphoto(list[position].uri, iv, context)
            if(list[position].fid == ""){
                v.tv_time.setTextColor(ContextCompat.getColor(context!!,R.color.blue))
                v.tv_time.text = "未上传"
            }else{
                v.tv_time.setTextColor(ContextCompat.getColor(context!!,R.color.black))
                Myutil.settv(Myutil.getdata(list[position].timestamp),v.tv_time)
            }
        }
        return v
    }

    override fun getItem(position: Int): Any {
        return list[position]
    }

    override fun getItemId(position: Int): Long {
        return position.toLong()
    }

    override fun getCount(): Int {
        if(isadd) {
            return list.size + 1
        }else{
            return list.size
        }
    }

    fun Allnotify(list : MutableList<PhotoBean>){
        this.list=list
        notifyDataSetChanged()
    }

}